package com.huatai.vba.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.huatai.vba.dal.model.VbaFileHandler;
import com.huatai.vba.execute.handler.ExecuteFactory;
import com.huatai.vba.service.VbaFileHandlerService;

@RestController
@RequestMapping(value = "")
public class FileHandlerController {

	@Autowired
	private ExecuteFactory executeFactory;

	@Autowired
	private VbaFileHandlerService vbaFileHandlerService;

	@RequestMapping(value = "queryVbaFileHandler", method = RequestMethod.GET)
	public ModelAndView login() {
		ModelAndView mv = new ModelAndView("vbafilehandler");
		List<VbaFileHandler> vbaFileHandlers = vbaFileHandlerService.queryVbaFileHandler(new VbaFileHandler());
		mv.addObject("vbaFileHandlers", vbaFileHandlers);
		return mv;
	}

	@RequestMapping(value = "execute", method = RequestMethod.GET)
	public ModelAndView execute(@RequestParam(required = false) String handlerId) {
		ModelAndView mv = new ModelAndView("success");
		try {
			VbaFileHandler handler = vbaFileHandlerService.queryById(handlerId);
			executeFactory.getExecuteService(handler).execute(handler);
		} catch (Exception ex) {
			mv = new ModelAndView("fail");
			ex.printStackTrace();
		}
		return mv;
	}

}
